package com.xiaoniu.forum.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoniu.forum.common.constant.ForumConstant;
import com.xiaoniu.forum.common.utils.PageUtils;
import com.xiaoniu.forum.common.utils.Query;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xiaoniu.forum.dao.BannerDao;
import com.xiaoniu.forum.entity.BannerEntity;
import com.xiaoniu.forum.service.BannerService;


@Service("bannerService")
public class BannerServiceImpl extends ServiceImpl<BannerDao, BannerEntity> implements BannerService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BannerEntity> page = this.page(
                new Query<BannerEntity>().getPage(params),
                new QueryWrapper<BannerEntity>()
        );
        return new PageUtils(page);
    }




    /**
     * ============================================================================================
     * 以下增删改方法，在调用时会清空redis中轮播图的缓存，为了保证redis和mysql的数据一致
     */

    @CacheEvict(value = "banners", allEntries=true)
    @Override
    public void addBanner(BannerEntity banner) {
        this.baseMapper.insert(banner);
    }

    @CacheEvict(value = "banners", allEntries=true)
    @Override
    public void updateBannerById(BannerEntity banner) {
        this.baseMapper.updateById(banner);
    }

    @CacheEvict(value = "banners", allEntries=true)
    @Override
    public void deleteBannerById(String id) {
        this.baseMapper.deleteById(id);
    }

    @CacheEvict(value = "banners", allEntries=true)
    @Override
    public void removeBannerByIds(List<String> ids) {
        this.baseMapper.deleteBatchIds(ids);
    }


}